// Dan Janssen
// SunSeeker Support
// 
// This program will read in one file that will then determine the change
// in altitude from one coordinate to the next and give the waypoints
// the file read in should come from the command line

// dataFile output:
// lat,lon,altitiude (this line is not shown)
// ##.##,##.##,###
// etc.

//	and

//Report output:
//Location1 -> Location 2 change in Alt is :


#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[]) {

	ifstream CFile;
	ofstream outFile ("Report.txt", ios::app);
	ofstream dataFile ("Data.txt");
	
	if ( argc != 2 ) {						// argc should be 2 for correct execution
		cout<<"usage: No filename\n";
		return 1;							//this will be left in for testing purposes 
	}
	else {
		
		CFile.open(argv[1]);					//open the file named at command line
	}

	if ( !CFile.is_open() ){					//make sure the file will open
		cout<<"Could not open coordinate file\n";
		return 0;
	}



//********************** all files are now open and ready for usage*************//

	int Alt;						//altitude raviables and difference
	int Alt2;
	int Diff;
	
	string lat, lon;						//coordinate variables
	
	string DummyLine;							// this is used to read the desc line as it normally is empty or "garbage"
	string locationStart;					//this is the starting location from point to point of altitude difference
	string locationStop;
	string city;

	getline(CFile,DummyLine,'\n');				//gets rid of first line of garbage data (start data is not kept)
	getline(CFile,DummyLine,'|');					// grab the W from the beginning of the file
	getline(CFile,lat,'|');							// grab the longitude
	getline(CFile,lon,'|');							// grab the latitude
	getline(CFile,locationStart,'|');			// location on the road
	getline(CFile,city,'|');						// city currently in 
	CFile >> Alt;										// the altitude is the only one we care to have a integer
					
	
	outFile << "All changes are given in meters (unless the user changed the ouput at the website to feet)\n";
	outFile << "A positive denotes going to a lower altitude, where a negative shows a climging altitude\n";
	dataFile << lat << "|" << lon << "|" << Alt <<"\n";			//data file entry to be read by plan

	while(!CFile.eof() ){
		
		
	
		getline(CFile,DummyLine,'|');							// grab the W from the beginning of the file
		getline(CFile,lat,'|');									// grab the longitude
		getline(CFile,lon,'|');									// grab the latitude
		getline(CFile,locationStop,'|');						// location on the road
		getline(CFile,city,'|');								// city currently in 
		CFile >> Alt2;												// the altitude is the only one we care to have a integer

		dataFile << lat << "|" << lon << "|" << Alt2 <<"\n";		//Data file entry

		// now to find the difference
		Diff = Alt - Alt2;
		outFile << locationStart <<" --> "<< locationStop << "\n\tChange in Evelation: " << Diff<<'\n';

		Alt = Alt2;	
						//move next location to new "beginning"
		locationStart = locationStop;				//swap the two for the next interation
		
		DummyLine=CFile.peek();						//peek if it's the end of not
		if (DummyLine != "\n")
			break;
	}// end of reading in from file
			
	outFile << "\n\n";									//seperate the legs
	
	CFile.close();
	outFile.close();
	dataFile.close();

return 0;
} 

//		CFile >> W >> comma >> lat >>comma>> lon >> comma ;	//to start the read process
//		getline(CFile,locationStop,',');			// need to use get line to go past spaces and use string
//		CFile >> comma;
//		getline(CFile,DummyLine,',');
//		CFile >> comma >> Alt2;
